/** 格式化工具函数 */
/** 日期 */
export function dateFormat(input, format = 'yyyy-MM-dd hh:mm:ss') {
  const date = new Date(input)
  if (String(date) === 'Invalid Date')
    return ''

  const makeReg = value => new RegExp(`${value}`)
  const keys = ['M+', 'd+', 'h+', 'm+', 's+', 'q+', 'S']
  const values = [
    date.getMonth() + 1,
    date.getDate(),
    date.getHours(),
    date.getMinutes(),
    date.getSeconds(),
    Math.floor((date.getMonth() + 3) / 3),
    date.getMilliseconds(),
  ]
  if (/(y+)/.test(format))
    format = format.replace(/(y+)/, (`${date.getFullYear()}`).substr(4 - RegExp.$1.length))

  let len = 0
  let key
  let val
  while (len < keys.length) {
    key = keys[len]
    val = values[len]
    if (makeReg(key).test(format))
      format = (format).replace(makeReg(key), (RegExp.$1.length === 1) ? val : (`00${val}`).substr((`${val}`).length))

    len++
  }
  return format
}

/** 时长 */
export function durationFormat(value) {
  let t = ''
  if (value > -1) {
    const hour = Math.floor(value / 3600)
    const min = Math.floor(value / 60) % 60
    const sec = value % 60
    if (hour > 0) {
      if (hour < 10)
        t = `0${hour}小时`

      else
        t = `${hour}小时`
    }

    if (min > 0) {
      if (min < 10)
        t += '0'

      t += `${min}分`
    }
    if (sec < 10)
      t += '0'

    t += `${sec}秒`
  }
  return t
}

/** 统一格式化 带宽和流量 */
type FormatType = 'flow' | 'bandwidth'
export function sizeFormat(size, type: FormatType = 'flow', toFixed = 2) {
  const K = 1000
  const sizeMap = {
    P: K * K * K * K * K,
    T: K * K * K * K,
    G: K * K * K,
    M: K * K,
    K,
  }
  const unit = type === 'flow' ? 'B' : 'bps'
  let roundSize = `0${unit}`
  let flag = true

  if (Number.isNaN(size))
    return roundSize
  if (!size || size < K) {
    return `${size}${unit}`
  }
  else {
    for (const obj in sizeMap) {
      if (size / sizeMap[obj] >= 1 && flag) {
        roundSize = (size / sizeMap[obj]).toFixed(toFixed) + obj + unit
        flag = false
      }
    }
    return roundSize
  }
}

export function getValue95(arr = []) {
  if (!arr || !arr.length)
    return sizeFormat(0)

  const valueArr = arr.map(item => item[1])
  const index95 = Math.ceil((valueArr.length * 5) / 100) - 1
  const value95 = valueArr.sort((a, b) => b - a)[index95]
  return sizeFormat(value95)
}

export function handleValues(arr = [], type = 'number') {
  function toFixed(value) {
    return value ? value.toFixed(2) : 0
  }
  const handlerMap = {
    number: value => toFixed(value),
    rate: sizeFormat,
    rtt: value => `${toFixed(value)}ms`,
    vsi_rtt: value => `${toFixed(value)}ms`,
    vpn_rtt: value => `${toFixed(value)}ms`,
    ext_rtt: value => `${toFixed(value)}ms`,
    loss: value => `${toFixed(value)}%`,
    vsi_lost: value => `${toFixed(value)}%`,
    vpn_lost: value => `${toFixed(value)}%`,
    ext_lost: value => `${toFixed(value)}%`,
    prate: value => `${toFixed(value)}个`,
  }

  const length = arr.length
  const { total, max, min } = arr.reduce((acc, item) => {
    const value = item[1] * 1
    acc.total += value
    acc.max = Math.max(acc.max, value)
    acc.min = Math.min(acc.min, value)
    return acc
  }, { total: 0, max: 0, min: 0 })

  const handler = handlerMap[type]
  return {
    max: handler(max),
    min: handler(min),
    avg: handler(total / length),
    v95: type === 'rate' ? getValue95(arr) : handler(0),
  }
}
